#   coding=utf-8
from django.shortcuts import render_to_response
from django.core.exceptions import ObjectDoesNotExist
from apps.joinus.models import JobType, Job
from apps.research.views import head_group

def join(request, lang, job_area):
    if lang != 'en' and lang != 'zh':
        return render_to_response('404.html', {'error': 'Unkown language error'})
    type_list = list(JobType.objects.all())
    if job_area is None:
        try:
            job_area = type_list[0].type_tag
        except IndexError:      #   type_list为空
            return render_to_response('join.html', {
                'current_item': 'join',
                'lang': lang,
                'head_group_list': head_group(),
                'url_en': '/en/join/',
                'url_zh': '/zh/join/'})
        except:
            return render_to_response('404.html')

    try:
        job_area = JobType.objects.get(type_tag=job_area)
    except ObjectDoesNotExist:
        return render_to_response('404.html', {'error': 'Invalid job type'})
    except:
        return render_to_response('404.html')

    job_list = list(Job.objects.filter(job_type=job_area))

    return render_to_response('join.html', {
        'current_item': 'join',
        'lang': lang,
        'url_en': '/en/join/'+str(job_area.type_tag)+'/',
        'url_zh': '/zh/join/'+str(job_area.type_tag)+'/',
        'head_group_list': head_group(),
        'job_type_list': type_list,
        'selected_job_type': job_area,
        'job_list': job_list})
